package com.teamin.web.aspect;

import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 用户请求记录
 * Created by e on 2017/9/24
 */
@Aspect
@Component
public class RequestAspect {

    Logger logger = LoggerFactory.getLogger(getClass());

    //Around 包含@Before() @After()
    @Around("execution( * com.teamin.web.controller.*.*(..))")
    public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {

        logger.info("in request Aspect");
        String args = StringUtils.join(pjp.getArgs(), ",");
        logger.info("args:{}", args);

        long start = System.currentTimeMillis();
        Object object = pjp.proceed();
        //logger.info("result:{}", JSONObject.fromObject(object));
        logger.info("耗时:{}", System.currentTimeMillis() - start);

        logger.info("out request Aspect");

        return object;
    }
}
